package com.practice.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.common.result.PageResult;
import com.practice.common.result.Result;
import com.practice.mapper.admin.AdminUserMapper;
import com.practice.pojo.admin.dto.AdminUserDTO;
import com.practice.pojo.admin.entity.AdminYddUser;
import com.practice.pojo.admin.entity.User;
import com.practice.pojo.admin.vo.AdminUserVO;
import com.practice.service.admin.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, User> implements AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    public Result list(AdminUserDTO adminUserDTO) {
        IPage<User> page = new Page<>(adminUserDTO.getPage(), adminUserDTO.getLimit());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        adminUserMapper.selectPage(page, lambdaQueryWrapper);

        return Result.success(new PageResult(
                page.getTotal(),
                page.getPages(),
                page.getSize(),
                page.getCurrent(),
                page.getRecords()
        ));
    }

    @Override
    public Result add(AdminUserDTO adminUserDTO) {
        User user = BeanUtil.copyProperties(adminUserDTO, User.class);
        user.setLoginName(adminUserDTO.getLogin_name());
        user.setRoleId(adminUserDTO.getRole_id().toString());
        user.setCreateDate(LocalDateTime.now());
        adminUserMapper.insert(user);
        return Result.success();
    }

    @Override
    public Result delete(String id) {
        adminUserMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public AdminUserVO rowInfo(String id) {
        AdminUserVO vo = adminUserMapper.rowInfo(id);
        String roleId = adminUserMapper.getByRoleId(id);
        Integer first = roleId.indexOf("[");
        Integer last = roleId.indexOf("]");
        String newRoleIds = roleId.substring(first+1, last)
                .replaceAll(" ","");
        String[] list = newRoleIds.split(",");

        vo.setRoleId(list);
        return vo;
    }

    @Override
    public void updateUser(AdminUserDTO dto) {
        User user = BeanUtil.copyProperties(dto, User.class);
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        user.setCreateDate(LocalDateTime.parse(dto.getCreate_date(),pattern));
        user.setDelFlag(dto.getDel_flag().toString());
        user.setLoginName(dto.getLogin_name());
        user.setRoleId(dto.getRole_id().toString());
        user.setUpdateDate(LocalDateTime.now());
        adminUserMapper.updateById(user);
    }
}
